# -*- coding: utf-8 -*-

"""The user interface for our app"""

import os,sys
import ConfigParser

# Import Qt modules
from PyQt4 import QtCore,QtGui

# Import the compiled UI module
from octo import Ui_Form
CFG_PATH = "etc/config.list"   #Config File Path
#config.list vars DEFAULT Values
window = None

# Create a class for our main window
class Main(QtGui.QWidget):
    def __init__(self, SOnTop,stclp):
        if SOnTop == "No":        
            QtGui.QWidget.__init__(self)
        else:
            QtGui.QWidget.__init__(self, None, QtCore.Qt.WindowStaysOnTopHint)
        # This is always the same
        self.ui=Ui_Form()
        self.ui.setupUi(self)
        # Window Icon
        icon = QtGui.QIcon("octopus.png")    
        self.setWindowIcon(icon)
        self.setWindowTitle("Octopy")     
        # Set the timer =)      
        self.timer = self.startTimer(1000) #self.killTimer(self.timer)
        # Clipboard Counter
        self.counter = 0  
        # Current X Y
        self.curY = 0
        self.curX = 0            
        # Buttons        
        self.cmd = []
        self.cmd.append(self.ui.cmd_1)
        self.cmd.append(self.ui.cmd_2)
        self.cmd.append(self.ui.cmd_3) 
        self.cmd.append(self.ui.cmd_4)
        self.cmd.append(self.ui.cmd_5)
        self.cmd.append(self.ui.cmd_6)                                        
        self.cmd.append(self.ui.cmd_7)
        self.cmd.append(self.ui.cmd_8)                        
        # Buffers
        self.tentacles = [""] * int(len(self.cmd)) 
        
## Events ##          
    def on_cmd_c_pressed(self): #Clear
        self.killTimer(self.timer)
        for i in range(0,len(self.tentacles)):
            self.tentacles[i] = ""  
                                  
        self.fillClp()      
        self.setClp("")
        self.counter = 0
        self.timer = self.startTimer(1000)            
            
            
    def on_cmd_1_pressed(self):
        t = self.ui.cmd_1.text()
        self.setClp(t)
        
    def on_cmd_2_pressed(self):
        t = self.ui.cmd_2.text()
        self.setClp(t)
        
    def on_cmd_3_pressed(self):
        t = self.ui.cmd_3.text()
        self.setClp(t)
        
    def on_cmd_4_pressed(self):
        t = self.ui.cmd_4.text()
        self.setClp(t)
        
    def on_cmd_5_pressed(self):
        t = self.ui.cmd_5.text()
        self.setClp(t)
        
    def on_cmd_6_pressed(self):
        t = self.ui.cmd_6.text()
        self.setClp(t)
        
    def on_cmd_7_pressed(self):                        
        t = self.ui.cmd_7.text()
        self.setClp(t)
        
    def on_cmd_8_pressed(self):                        
        t = self.ui.cmd_8.text()
        self.setClp(t)        
                          
    def hideEvent(self,event): # Capture close and minimize events 
        self.curX = self.x()
        self.curY = self.y()
        
    def keyPressEvent(self,ev):
        if ev.key() == QtCore.Qt.Key_Escape:
            self.hide()
                
    def showEvent(self,ev):
        self.move(self.curX,self.curY)    
        self.fillClp()   
        
    def timerEvent(self,ev):
        c = self.getClp()           
        if c:                        
            #print c, self.counter 
            self.tentacles[self.counter] = c
            if self.counter < 7:
                self.counter += 1
            else:
                self.counter = 0
            
            self.fillClp()

## Functions ##
            
    def fillClp(self):
        for i in range(0,len(self.tentacles)):
           self.cmd[i].setText(self.tentacles[i])
           self.cmd[i].setToolTip(self.tentacles[i])      
                        
    def getClp(self):
        clp = QtGui.QApplication.clipboard() 
        c = clp.text()                        
        if c not in self.tentacles:
            return c
        else:
            return None
    
    def setClp(self, t):               
        clp = QtGui.QApplication.clipboard() 
        clp.setText(t)        
              
              
class SystemTrayIcon(QtGui.QSystemTrayIcon):
    def __init__(self, icon, parent=None):
        QtGui.QSystemTrayIcon.__init__(self, icon, parent)
        menu = QtGui.QMenu(parent)                
        # Actions
        self.action_quit = QtGui.QAction("Quit", self)
        self.action_about = QtGui.QAction("About Octopy", self)
        # Add actions to menu
        menu.addAction(self.action_about)
        menu.addSeparator()
        menu.addAction(self.action_quit)
        # Connect menu with signals
        self.connect(self.action_about, QtCore.SIGNAL("triggered()"), self.about)       
        self.connect(self.action_quit, QtCore.SIGNAL("triggered()"), self.quit) 
        # Other signals
        traySignal = "activated(QSystemTrayIcon::ActivationReason)"
        QtCore.QObject.connect(self, QtCore.SIGNAL(traySignal), self.icon_activated)
        # Create Menu               
        self.setContextMenu(menu)
                
    def quit(self):
        w = QtGui.QWidget()
        msg = QtGui.QMessageBox      
        reply = msg.question(w, 'Confirm Action',"Are you sure to quit?", QtGui.QMessageBox.Yes, QtGui.QMessageBox.No)
        if reply == QtGui.QMessageBox.Yes:           
            QtGui.QApplication.quit()

    def about(self):            
        w = QtGui.QWidget()
        msg = QtGui.QMessageBox
        msg.information(w, 'About', "Octopy Multi-Clipboard Manager\n Developed by mRt.")
                
    def icon_activated(self, reason):
        if reason == QtGui.QSystemTrayIcon.DoubleClick:
            window.show()


def main():
    # Again, this is boilerplate, it's going to be the same on 
    # almost every app you write
    app = QtGui.QApplication(sys.argv)        
    # TrayIcon
    w = QtGui.QWidget()
    icon = QtGui.QIcon("octopus.png")          
    trayIcon = SystemTrayIcon(icon, w)
    trayIcon.show()    
    trayIcon.setToolTip("Octopy Multi-Clipboard Manager")        
    # Read Ini
    cfg = ConfigParser.ConfigParser()
    cfg.read(CFG_PATH)      
    stclp = cfg.get("Main","Static")    
    SOnTop = cfg.get("Main","StayOnTop")       
    # Main Window
    global window           
    window=Main(SOnTop,stclp)
    window.show()    
    window.setWindowTitle("Octopy")
    app.setQuitOnLastWindowClosed(0)
    sys.exit(app.exec_())    
        
       
if __name__ == "__main__":
    main()
    
